//
//  ZGSDestinationCityController.m
//  Tour
//
//  Created by dllo on 15/10/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ZGSDestinationCityController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "ZGSDestinationCithCell.h"
#import "AdvanceEdit.h"
#import "BaseModel.h"
#import "ZGSDestinationCell.h"
#import "ZGSDestinaDetailsViewController.h"
#import "ZGSMoreDIscountViewController.h"
#import "SaveTool.h"
#import "ZGSDataBaseHandle.h"
#import "ZGSQQQQQQQQQQ.h"
#import "ZGSCountryViewController.h"
#import "ScrollWebViewController.h"
//#import ""
@interface ZGSDestinationCityController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UIView *headView;
@property (nonatomic, retain)UIImageView *scrollImageView;
@property (nonatomic, retain)NSMutableArray *arrPhoto;
@property (nonatomic, retain)NSMutableArray *arrHotCity;
@property (nonatomic, retain)NSMutableArray *arrNewDiscount;
@property (nonatomic, retain)MBProgressHUD *HUD;

//@property (nonatomic, retain)UILabel *labelCountry;
@end

@implementation ZGSDestinationCityController

- (void)dealloc{
//    @property (nonatomic, copy)NSString *urlStr;
//    @property (nonatomic, copy)NSString *city;
//    @property (nonatomic, copy)NSString *cityid;
//    @property (nonatomic, copy)NSString *name;
//    
//    @property (nonatomic, retain)ZGSQQQQQQQQQQ *movie;

    [_scrollView release];
    [_tableView release];
    [_collectionView release];
    [_headView release];
    [_scrollImageView release];
    [_arrPhoto release];
    [_arrHotCity release];
    [_arrNewDiscount release];
    [_HUD release];
    [_urlStr release];
    [_city release];
    [_cityid release];
    [_name release];
    [_movie release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBarHidden = YES;

    self.title = self.name;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.headView = [[UIView alloc] init];
    self.headView.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 2 - 100 + 60 + HEIGHT / 2 - 100 + 40);
    self.headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.tableView addSubview:self.headView];
    [_headView release];
    
    
//     self.movie = [[ZGSQQQQQQQQQQ alloc] init];
    
    
    SelectMovie result = [[ZGSDataBaseHandle shareDataBaseHandle] isHaveIntable:self.movie];
    NSLog(@"%@", self.movie);
    
    
    if (result == Intable) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消收藏" style:UIBarButtonItemStylePlain target:self action:@selector(clickCollect:)];
    }else if(result == NotInTable) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(clickCollect:)];
    }else{
        NSLog(@"出错");
    }
    

    
   
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.tableView registerClass:[ZGSDestinationCell class] forCellReuseIdentifier:@"tableReuseo"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.headView;
    self.tableView.rowHeight = 150;
    [self.view addSubview:self.tableView];
    [_tableView release];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT -  self.navigationController.navigationBar.frame.size.height, WIDTH, self.navigationController.navigationBar.frame.size.height)];
    [self.view addSubview:label];
    label.text = @"祝你旅途愉快!";
    label.textAlignment = 1;
    [label release];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 2 - 100)];
    [self.headView addSubview:self.scrollView];
    
    UIButton *labelInspetMore = [UIButton buttonWithType:UIButtonTypeSystem];
    labelInspetMore.frame = CGRectMake(0, HEIGHT / 2 - 100 + 10, WIDTH, 40);

    [labelInspetMore setTitle:@"查看更多城市" forState:UIControlStateNormal];
    [labelInspetMore addTarget:self action:@selector(moreCityAction:) forControlEvents:UIControlEventTouchUpInside];
 
    [self.headView addSubview:labelInspetMore];
    [labelInspetMore release];

    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 10, (HEIGHT / 2 - 100) / 2 - 10);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 2 - 100 + 60, WIDTH, HEIGHT / 2 - 100) collectionViewLayout:flowLayout];
//    self.collectionView.backgroundColor = [UIColor cyanColor];
    [self.collectionView registerClass:[ZGSDestinationCithCell class] forCellWithReuseIdentifier:@"reuseo"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.headView addSubview:self.collectionView];
    [_collectionView release];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, HEIGHT / 2 - 100 + 60 + HEIGHT / 2 - 100, WIDTH, 40);
    [button setTitle:@"更多超值自由行Yeah!" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:button];
    

    
    
    
    self.arrPhoto = [NSMutableArray array];
    self.arrHotCity = [NSMutableArray array];
    self.arrNewDiscount = [NSMutableArray array];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.dimBackground = YES;
    
    [self creatDate];

    NSLog(@"%@", self.cityid);
    
    
    
    
}

- (void)clickCollect:(UIBarButtonItem *)button{
//    [[ZGSDataBaseHandle shareDataBaseHandle] createTable];
    if ([button.title isEqualToString:@"收藏"]) {
//        [[ZGSDataBaseHandle shareDataBaseHandle] createTable];


        [[ZGSDataBaseHandle shareDataBaseHandle] saveMovieInDB:self.movie];
        NSMutableArray *arr = [[ZGSDataBaseHandle shareDataBaseHandle] selectAllMovie];
        NSLog(@"%@", arr);

        button.title = @"取消收藏";
    }else{
        [[ZGSDataBaseHandle shareDataBaseHandle] deleteMovieInDB:self.movie];
        button.title = @"收藏";
        
    }

}

- (void)creatDate{
#if 1
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&lat=38.88269399700311&lon=121.5393187001478&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone7%%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%%25208.4.1&v=1", self.cityid];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;

        
        NSArray *arrPhoto = dic[@"data"][@"photos"];
        for (NSString *str in arrPhoto) {
            
            [self.arrPhoto addObject:str];
        }
        self.arrHotCity = [ZGSDestinationCity baseModelArr:dic[@"data"][@"hot_city"]];
//        NSLog(@"666666%@", [self.arrHotCity[0] photo]);
        
        NSArray *arrNewDiscount = [ZGSDestinationCity baseModelArr:dic[@"data"][@"new_discount"]];
        for (ZGSDestinationCity *desCity in arrNewDiscount) {
            [self.arrNewDiscount addObject:desCity];
        }
//        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 2 - 100)];
        self.scrollView.contentSize = CGSizeMake(WIDTH * self.arrPhoto.count, 0);
        self.scrollView.backgroundColor = [UIColor whiteColor];
        self.scrollView.pagingEnabled = YES;
        for (NSInteger i = 0; i < self.arrPhoto.count; i++) {
            UIImageView *imageView= [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT / 2 - 100 + 100)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.arrPhoto[i]]];
            [self.scrollView addSubview:imageView];
        }
//        [self.headView addSubview:self.scrollView];
//        NSLog(@"%@", self.arrNewDiscount);
        [self.tableView reloadData];
    
        [self.collectionView reloadData];
        
       
        
//        [self.movie setValuesForKeysWithDictionary:dic[@"data"]];

        self.HUD.hidden = YES;
        self.navigationController.navigationBarHidden = NO;
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
#endif
}

- (void)moreCityAction:(UIButton *)button{
    ZGSDestinaDetailsViewController *detailVC = [[ZGSDestinaDetailsViewController alloc] init];
    detailVC.cityId = self.cityid;
//    self.cityid
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];

}

- (void)moreAction:(UIButton *)button{
    ZGSMoreDIscountViewController *disVC = [[ZGSMoreDIscountViewController alloc] init];
    disVC.cityId = self.cityid;
    disVC.name = self.name;
    disVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:disVC animated:YES];
    
    
    
}
- (void)click:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrNewDiscount.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDestinationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableReuseo" forIndexPath:indexPath];
    NSLog(@"%@", [self.arrNewDiscount[indexPath.row] price]);
    NSString *str = [[self.arrNewDiscount[indexPath.row] price] substringWithRange:NSMakeRange(4, [self.arrNewDiscount[indexPath.row] price].length - 4)];
    NSMutableString *tmp = [NSMutableString stringWithFormat:@"%@", str];
    NSString *strTmp = [tmp stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.labelTitle.text = [self.arrNewDiscount[indexPath.row] title];
    cell.labelMoney.text = strTmp;
    cell.labelPrice.text = [self.arrNewDiscount[indexPath.row] priceoff];
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:[self.arrNewDiscount[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrHotCity.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDestinationCithCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseo" forIndexPath:indexPath];
    [cell.imageCity sd_setImageWithURL:[NSURL URLWithString:[self.arrHotCity[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
    NSLog(@"%@", [self.arrHotCity[indexPath.row] photo]);
    cell.labelCity.text = [self.arrHotCity[indexPath.row] cnname];
//    NSLog(@"111111231312312321");
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    ZGSCountryViewController *zgsCountry = [[ZGSCountryViewController alloc] init];
//    NSString *str = [NSString stringWithFormat:@"%@", [self.arrNewDiscount[indexPath.row] countryid]];
//    http://z.qyer.com/deal/52500/
    NSString *web = [NSString stringWithFormat:@"http://z.qyer.com/deal/%@/", [self.arrNewDiscount[indexPath.row] countryid]];
    ScrollWebViewController *webVC = [[ScrollWebViewController alloc] init];
    webVC.urlStr = web;
    [self presentViewController:webVC animated:YES completion:^{
        
        
    }];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ZGSCountryViewController *zgsCountry = [[ZGSCountryViewController alloc] init];
    NSString *str = [NSString stringWithFormat:@"%@", [self.arrHotCity[indexPath.row] countryid]];
    zgsCountry.cityid = str;
    zgsCountry.name = [self.arrHotCity[indexPath.row] cnname];
    [self.navigationController pushViewController:zgsCountry animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
